/**   
* @Title: SlipOpenCloseButton.java 
* @author zengshuai shi   
* @date 2015年7月31日 上午10:34:57 
*/
package com.onstar.cn.weight;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

/**
 * @ClassName: SlipOpenCloseButton
 * @Description: TODO
 * @author zengshuai shi
 * @date 2015年7月31日 上午10:34:57
 */
public class SlipOpenCloseButton extends View implements OnTouchListener {
	private boolean NowChoose = false;// 记录当前按钮是否打开,true为打开,flase为关闭
	private boolean OnSlip = false;// 记录用户是否在滑动的变量
	private float down_x, now_x;// 按下时的x,当前的x,
	private Rect btn_on, btn_off;// 打开和关闭状态下,游标的Rect
	private boolean isChecked = false;
	private OnChangedListener onChangeListener;

	private Bitmap open_on, open_off, slip_btn;

	public SlipOpenCloseButton(Context context) {
		super(context);
		init();
	}

	public SlipOpenCloseButton(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public SlipOpenCloseButton(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		init();
	}

	/**
	 * 设置开关背景
	 * 
	 * @param openOnResId
	 * @param openOffResId
	 * @param openSlipBtn
	 */
	public void setOpenBg(int openOnResId, int openOffResId, int openSlipBtn) {
		open_on = BitmapFactory.decodeResource(getResources(), openOnResId);
		open_off = BitmapFactory.decodeResource(getResources(), openOffResId);
		slip_btn = BitmapFactory.decodeResource(getResources(), openSlipBtn);
	}

	/**
	 * 初始化
	 */
	private void init() {
		// 载入图片资源
		open_on = BitmapFactory.decodeResource(getResources(), R.drawable.weight_open_off);
		open_off = BitmapFactory.decodeResource(getResources(), R.drawable.weight_open_on);
		slip_btn = BitmapFactory.decodeResource(getResources(), R.drawable.weight_btn_slip);
		// 获得需要的Rect数据
		btn_on = new Rect(0, 0, slip_btn.getWidth(), slip_btn.getHeight());
		btn_off = new Rect(open_off.getWidth() - slip_btn.getWidth(), 0, open_off.getWidth(), slip_btn.getHeight());
		setOnTouchListener(this);
	}

	/**
	 * (non-Javadoc)
	 * <p>
	 * Title: onDraw
	 * </p>
	 * <p>
	 * Description:
	 * </p>
	 * 
	 * @param canvas
	 * @see android.view.View#onDraw(android.graphics.Canvas)
	 */
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		Matrix matrix = new Matrix();
		Paint paint = new Paint();
		float x;
		// 滑动到前半段与后半段的背景不同,在此做判断
		if (now_x < (open_on.getWidth() / 2)) {
			// 关闭背景
			x = now_x - slip_btn.getWidth() / 2;
			canvas.drawBitmap(open_off, matrix, paint);
		} else {
			// 打开背景
			x = open_on.getWidth() - slip_btn.getWidth() / 2;
			canvas.drawBitmap(open_on, matrix, paint);
		}
		// 是否是在滑动状态
		if (OnSlip) {
			// 是否划出指定范围
			if (now_x >= open_on.getWidth())
				x = open_on.getWidth() - slip_btn.getWidth() / 2;
			else if (now_x < 0)
				x = 0;
			else
				x = now_x - slip_btn.getWidth() / 2;
		} else {
			// 非滑动状态
			if (NowChoose) {
				x = btn_off.left;
				canvas.drawBitmap(open_on, matrix, paint);
			} else {
				x = btn_on.left;
			}
		}
		if (isChecked) {
			canvas.drawBitmap(open_on, matrix, paint);
			x = btn_off.left;
			isChecked = !isChecked;
		}
		if (x < 0) {
			x = 0;
		} else if (x > open_on.getWidth() - slip_btn.getWidth()) {
			x = open_on.getWidth() - slip_btn.getWidth();
		}
		canvas.drawBitmap(slip_btn, x, 0, paint);
	}

	/**
	 * (non-Javadoc)
	 * <p>
	 * Title: onTouch
	 * </p>
	 * <p>
	 * Description:
	 * </p>
	 * 
	 * @param v
	 * @param event
	 * @return
	 * @see android.view.View.OnTouchListener#onTouch(android.view.View,
	 *      android.view.MotionEvent)
	 */
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_MOVE:// 滑动
			now_x = event.getX();
			break;
		case MotionEvent.ACTION_DOWN:// 按下
			if (event.getX() > open_on.getWidth() || event.getY() > open_on.getHeight())
				return false;
			OnSlip = true;
			down_x = event.getX();
			now_x = down_x;
			break;
		case MotionEvent.ACTION_UP:// 松开
			OnSlip = false;
			boolean LastChoose = NowChoose;
			NowChoose = event.getX() >= (open_on.getWidth() / 2);
			if (onChangeListener != null && (LastChoose != NowChoose))
				onChangeListener.OnChanged(NowChoose, this);
			break;
		default:
		}
		// 重绘
		invalidate();
		return true;
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		setMeasuredDimension(open_on.getWidth(), open_on.getHeight());
	}

	public void setIsChecked(boolean isChecked) {
		this.isChecked = isChecked;
		this.NowChoose = isChecked;
		if (isChecked) {
			now_x = open_on.getWidth();
		} else {
			now_x = 0;
		}
		invalidate();
	}

	public void setOnChangeListener(OnChangedListener onChangeListener) {
		this.onChangeListener = onChangeListener;
	}

	public interface OnChangedListener {
		void OnChanged(boolean isChecked, View view);
	}

	public void releaseBitmap() {
		if (open_on != null && !open_on.isRecycled()) {
			open_on.recycle();
		}
		if (open_off != null && !open_off.isRecycled()) {
			open_off.recycle();
		}
		if (slip_btn != null && !slip_btn.isRecycled()) {
			slip_btn.recycle();
		}
	}

	public boolean isChecked() {
		return NowChoose;
	}
}
